package com.yuyou.fn.platform.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.platform.entity.Oauth;
import com.yuyou.fn.platform.mapper.OauthMapper;
import com.yuyou.fn.platform.service.IOauthService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * Oauth 表数据服务层接口实现类
 *
 */
@Service
public class OauthServiceImpl extends SuperServiceImpl<OauthMapper, Oauth,Long> implements IOauthService {


    @Override
    public Oauth findByRoleOpenId(Integer role , String openId) {
        Example<Oauth> example = Oauth.createExample();
        example.orderBy("oauthId").desc();
        example.createCriteria().andEqualTo("openId" , openId ).andEqualTo("role" , role );
        return findOneByExample(example);
    }

    @Override
    public void saveOauth(Integer role , Long userId, String openId, Integer platform) {
        Example<Oauth> example=Oauth.createExample();
        example.createCriteria().andEqualTo("openId",openId).andEqualTo("whichPlatform",platform).andEqualTo("userId",userId).andEqualTo("role",role);
        Oauth check=this.findOneByExample(example);
        if (check==null){

            Oauth oauth = new Oauth();
            oauth.setOpenId(openId);
            oauth.setWhichPlatform(platform);
            oauth.setUserId(userId);
            oauth.setRole(role);
            insertSelective(oauth);
        }
    }

    @Override
    public void unbind(String openId, Long userId, int role) {
        if(openId == null || userId == null ){
            throw new NullPointerException();
        }
        Example<Oauth> example = Oauth.createExample();
        example.createCriteria().andEqualTo("openId",openId ).andEqualTo("userId" , userId ).andEqualTo("role",role );
        deleteByExample(example);
    }

    @Override
    public void unbind(String openId, int role) {
        if(StringUtils.isEmpty(openId)){
            throw new NullPointerException();
        }
        Example<Oauth> example = Oauth.createExample();
        example.createCriteria().andEqualTo("openId",openId ).andEqualTo("role",role );
        deleteByExample(example);
    }

    @Override
    public void deleteByUserIdRole(Long userId, int role) {
        if( userId == null ){
            throw new NullPointerException();
        }
        Example<Oauth> example = Oauth.createExample();
        example.createCriteria().andEqualTo("userId" , userId ).andEqualTo("role",role );
        deleteByExample(example);
    }

    @Override
    public Oauth findByUserId(Long userId) {
        Example<Oauth> example = Oauth.createExample();
        example.createCriteria().andEqualTo("userId",userId );
        return findOneByExample(example);
    }

    @Override
    public void updateUserId(Long userId , String openId, Integer platform) {
        Example<Oauth> example = Oauth.createExample();
        example.createCriteria().andEqualTo("openId",openId ).andEqualTo("role" , platform );
        Oauth oauth = new Oauth();
        oauth.setUserId(userId);
        updateSelectiveByExample(oauth,example);

    }

    @Override
    public List<Oauth> findByUserIdRole(long userId, int role) {

        Example<Oauth> example = Oauth.createExample();
        example.createCriteria().andEqualTo("userId",userId ).andEqualTo("role",role);
        example.orderBy("oauthId").desc();
        return findListByExample(example);
    }
}